package com.banyou.banyou.main.history;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.banyou.banyou.R;
import com.banyou.banyou.adapter.HistoryAdapter;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.bean.main.my.MyRecord;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 类描述：
 * 创建人：Random
 * 修改时间：
 * 修改备注：
 */

public class HistoryActivity extends BaseActivity<HistoryPresenter> implements HistoryContract.View {

    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.rv)
    RecyclerView mRv;

    private int mStart=0, mLimit=15,mTotal;
    private HistoryAdapter mHistoryAdapter;
    private List<MyRecord> mList = new ArrayList<>();

    @Override
    protected HistoryPresenter initPresenter() {
        return new HistoryPresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_history;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter.initPresenter(this, new HistoryModel());
        ButterKnife.bind(this);
        mHistoryAdapter = new HistoryAdapter(mList, this);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRv.setLayoutManager(manager);
        mRv.setAdapter(mHistoryAdapter);
        final Map<String, String> map = new HashMap<>();
        map.put("userId", getSharedPreference().getString("userId", "1"));
        map.put("start", mStart + "");
        map.put("limit", mLimit + "");
        mPresenter.getData(map);
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //屏幕中最后一个可见子项的position
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                //当前屏幕所看到的子项个数
                int visibleItemCount = layoutManager.getChildCount();
                //当前RecyclerView的所有子项个数
                int totalItemCount = layoutManager.getItemCount();
                if (visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && newState == RecyclerView.SCROLL_STATE_IDLE&&mLimit*(mStart+1)<=mTotal) {
                    map.put("start", "" + mStart++);
                    mPresenter.getData(map);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }


    @Override
    protected String pageName() {
        return "提款历史";
    }


    @OnClick(R.id.back_btn)
    public void onClick() {
        finish();
    }

    @Override
    public void getDataSuccess(List<MyRecord> list) {
        mList.clear();
        mTotal=list.get(0).getTotal();
        list.remove(0);
        mList.addAll(list);
        mHistoryAdapter.notifyDataSetChanged();
    }
}
